#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_mixer.h>
#include <assert.h>
#include "Solution.h"
void jouer (int *pcontinuer, int Nummap)
{
int continuer2=1,*localx,*localy,rep,akari[100][100],solakari[100][100];
char chaine[100];
SDL_Surface *ecran= NULL,*fond_base= NULL,*localiser= NULL;


SDL_Rect position_base,position_localiser;
if (Nummap==3)
{
position_localiser.x=24+93;
}
else
{
position_localiser.x=24;
}
position_localiser.y=147;
localx=&position_localiser.x;
localy=&position_localiser.y;


RempAkari(akari,Nummap);
RempSol(solakari,Nummap);


SDL_Event event;
SDL_Init(SDL_INIT_EVERYTHING);
position_base.x=0;
position_base.y=0;
ecran= SDL_SetVideoMode(700, 719, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
switch (Nummap){
case 1 : strcpy(chaine,"map1.jpg");break;
case 2 : strcpy(chaine,"map2.jpg");break;
case 3 : strcpy(chaine,"map3.jpg");break;
}
fond_base=IMG_Load(chaine);
SDL_BlitSurface(fond_base,NULL,ecran,&position_base);
localiser=IMG_Load("localiser.jpg");
SDL_BlitSurface(localiser,NULL,ecran,&position_localiser);
SDL_Flip(ecran);

while (continuer2)
{

SDL_WaitEvent(&event);
       switch (event.type)
    {

    case SDL_QUIT : continuer2=0;*pcontinuer=0;break;
    case SDL_KEYDOWN :
switch (event.key.keysym.sym)
{
case SDLK_RIGHT :rep=4;break;
case SDLK_DOWN :rep=1;break;
case SDLK_UP  :rep=3;break;
case SDLK_LEFT :rep=2;break;
case SDLK_RETURN : rep=0;
if (VerifFlemme(akari,position_localiser)==0)
{
DessinHori(ecran,position_localiser,localx,localy,akari);DessinVerti(ecran,position_localiser,localx,localy,akari);
Rempconsole(position_localiser,akari);
}
break;

case SDLK_c :
if (Verifpartie(akari,solakari)==0)
{
SDL_FreeSurface(ecran);
SDL_FreeSurface(localiser);
SDL_FreeSurface(fond_base);
Resultat(0,1,Nummap);
}
else
{
SDL_FreeSurface(ecran);
SDL_FreeSurface(localiser);
SDL_FreeSurface(fond_base);
Resultat(1,1,Nummap);
}
break;
}
if (rep!=0)
{
EffacerLocaliser(ecran,position_localiser,akari);
DeplacerLocaliser(ecran,position_localiser,localx,localy,rep,akari);
}
break;

}



}
SDL_FreeSurface(ecran);
SDL_FreeSurface(localiser);
SDL_FreeSurface(fond_base);
SDL_Quit();

}

int VerifFlemme(int akari[100][100], SDL_Rect position_localiser)
{
int i,j,c,l,verif=0;
c=((position_localiser.x)-24)/93;
l=((position_localiser.y)-147)/79;
for (j=(c+1);j<=6;j++)
{
if (akari[l][j]==99)
{
if (akari[l][j+1]==5)
{
verif=1;
break;
}
}
else if (akari[l][j]==5)
{
verif=1;
break;
}
else
{
break;
}
}

for (j=(c-1);j>=0;j--)
{
if (akari[l][j]==99)
{
if (akari[l][j-1]==5)
{
verif=1;
break;
}
}
else if (akari[l][j]==5)
{
verif=1;
break;
}
else
{
break;
}
}

for (i=(l+1);i<=6;i++)
{
if (akari[i][c]==99)
{
if (akari[i+1][c]==5)
{
verif=1;
break;
}
}
else if (akari[i][c]==5)
{
verif=1;
break;
}
else
{
break;
}
}

for (i=(l-1);i>=0;i--)
{
if (akari[i][c]==99)
{
if (akari[i-1][c]==5)
{
verif=1;
break;
}
}
else if (akari[i][c]==5)
{
verif=1;
break;
}
else
{
break;
}
}

return verif;
}


int Verifpartie(int akari[100][100], int solakari[100][100])
{
int i,j,verif=0;
for (i=0;i<=6;i++)
{
for (j=0;j<=6;j++)
{
if (akari[i][j]!=solakari[i][j])
{
verif=1;
break;
}
}
}
return verif;
}

void DeplacerLocaliser(SDL_Surface *ecran,SDL_Rect position_localiser, int *localx, int *localy, int rep, int akari[100][100])
{
SDL_Surface *localiser;
localiser=IMG_Load("localiser.jpg");
if (Verifcube(position_localiser,akari,rep)==0)
{
switch (rep)
{
case 1 :
if ((700-position_localiser.y)>=150)
{
(*localy)+=79;
}
break;
case 2 :
if ((719-position_localiser.x)<650)
{
(*localx)-=93;
}
break;
case 3 :
if ((700-position_localiser.y)<600)
{
(*localy)-=79;
}
break;
case 4 :
if ((719-position_localiser.x)>=150)
{
(*localx)+=93;
}
break;
}
}
position_localiser.x=*localx;
position_localiser.y=*localy;
SDL_BlitSurface(localiser,NULL,ecran,&position_localiser);
SDL_Flip(ecran);
SDL_FreeSurface(localiser);

}


int Verifcube(SDL_Rect position_localiser, int akari[100][100], int pos)
{
int verif=0,c,l;
c=((position_localiser.x)-24)/93;
l=((position_localiser.y)-147)/79;
switch (pos){
case 1 :
if (akari[l+1][c]!=10 && akari[l+1][c]!=99 && akari[l+1][c]!=5)
{
verif=1;
}
break;
case 2 :
if (akari[l][c-1]!=10&& akari[l][c-1]!=99 && akari[l][c-1]!=5)
{
verif=1;
}
break;
case 3 :
if (akari[l-1][c]!=10&& akari[l-1][c]!=99 && akari[l-1][c]!=5)
{
verif=1;
}
break;
case 4 :
if (akari[l][c+1]!=10&& akari[l][c+1]!=99 && akari[l][c+1]!=5)
{
verif=1;
}
break;
}
return verif;
}


int VerifSurface(SDL_Surface *ecran, SDL_Rect position_localiser, int *localx, int *localy, int pos)
{
int verif=0;
switch (pos)
{
case 1 :
if ((700-position_localiser.y)<150)
{
verif=1;
}
break;
case 2 :
if ((719-position_localiser.x)>650)
{
verif=1;
}
break;
case 3 :
if ((700-position_localiser.y)>550)
{
verif=1;
}
break;
case 4 :
if ((719-position_localiser.x)<150)
{
verif=1;
}
}
return verif;
}


void DessinHori(SDL_Surface *ecran, SDL_Rect position_localiser, int *localx, int *localy, int akari[100][100])
{
SDL_Rect position_Hori;
SDL_Surface *eclair=NULL,*lamp=NULL;
eclair=IMG_Load("eclair.jpg");
position_Hori.x=position_localiser.x;
position_Hori.y=position_localiser.y;
lamp=IMG_Load("lamp.jpg");
SDL_BlitSurface(lamp,NULL,ecran,&position_localiser);
SDL_Flip(ecran);
while ((VerifSurface(ecran,position_Hori,localx,localy,4)==0) && (Verifcube(position_Hori,akari,4)==0))
{
position_Hori.x+=93;
SDL_BlitSurface(eclair,NULL,ecran,&position_Hori);
SDL_Flip(ecran);
}

position_Hori.x=position_localiser.x;
position_Hori.y=position_localiser.y;

while ((VerifSurface(ecran,position_Hori,localx,localy,2)==0) && (Verifcube(position_Hori,akari,2)==0))
{
position_Hori.x-=93;
SDL_BlitSurface(eclair,NULL,ecran,&position_Hori);
SDL_Flip(ecran);
}
SDL_FreeSurface(eclair);
}

void DessinVerti(SDL_Surface *ecran, SDL_Rect position_localiser, int *localx, int *localy, int akari[100][100])
{
SDL_Rect position_Verti;
SDL_Surface *eclair;
eclair=IMG_Load("eclair.jpg");
position_Verti.x=position_localiser.x;
position_Verti.y=position_localiser.y;
while ((VerifSurface(ecran,position_Verti,localx,localy,1)==0) && (Verifcube(position_Verti,akari,1)==0))
{
position_Verti.y+=79;
SDL_BlitSurface(eclair,NULL,ecran,&position_Verti);
SDL_Flip(ecran);
}
position_Verti.x=position_localiser.x;
position_Verti.y=position_localiser.y;
while ((VerifSurface(ecran,position_Verti,localx,localy,3)==0) && (Verifcube(position_Verti,akari,3)==0))
{
position_Verti.y-=79;
SDL_BlitSurface(eclair,NULL,ecran,&position_Verti);
SDL_Flip(ecran);
}
SDL_FreeSurface(eclair);
}

void EffacerLocaliser (SDL_Surface *ecran, SDL_Rect position_localiser, int akari[100][100])
{
SDL_Surface *effacer=NULL;
SDL_Rect position_effacer;
position_effacer.x=position_localiser.x;
position_effacer.y=position_localiser.y;
if (Localiserconsole(akari,position_localiser)==99)
{
effacer=IMG_Load("eclair.jpg");
}
else if (Localiserconsole(akari,position_localiser)==5)
{
effacer=IMG_Load("lamp.jpg");
}
else
{
effacer=IMG_Load("supp.jpg");
}
SDL_BlitSurface(effacer,NULL,ecran,&position_effacer);
SDL_Flip(ecran);
SDL_FreeSurface(effacer);
}

int Localiserconsole(int akari[100][100], SDL_Rect position_localiser)
{
int c,l;
c=((position_localiser.x)-24)/93;
l=((position_localiser.y)-147)/79;
if (akari[l][c]==99)
{
return 99;
}
else if (akari[l][c]==5)
{
return 5;
}
}

void Rempconsole(SDL_Rect position_localiser, int akari[100][100])
{
int c,l,i,j;
c=((position_localiser.x)-24)/93;
l=((position_localiser.y)-147)/79;
for (j=(c+1);j<=6;j++)
{
if (akari[l][j]==10 || akari[l][j]==99)
{
akari[l][j]=99;
}
else
{
break;
}
}
for (j=(c-1);j>=0;j--)
{
if (akari[l][j]==10 || akari[l][j]==99)
{
akari[l][j]=99;
}
else
{
break;
}
}

for (i=(l+1);i<=6;i++)
{
if (akari[i][c]==10 || akari[i][c]==99)
{
akari[i][c]=99;
}
else
{
break;
}
}
for (i=(l-1);i>=0;i--)
{
if (akari[i][c]==10 || akari[i][c]==99)
{
akari[i][c]=99;
}
else
{
break;
}
}
akari[l][c]=5;
}

void RempAkari (int akari[100][100], int MAP)
{
int i,j;
for (i=0;i<=6;i++)
{
for (j=0;j<=6;j++)
{
akari[i][j]=10;
}
}
switch (MAP){
case 1 :
akari[1][0]=98; // carreau noir vide => 98
akari[3][0]=98;
akari[6][1]=3;
akari[2][2]=98;
akari[4][2]=98;
akari[0][3]=1;
akari[6][3]=2;
akari[2][4]=98;
akari[4][4]=3;
akari[0][5]=0;
akari[3][6]=98;
akari[5][6]=98;
break;
case 2 :
akari[1][0]=98;
akari[6][1]=0;
akari[2][2]=3;
akari[4][2]=0;
akari[3][3]=98;
akari[2][4]=3;
akari[4][4]=98;
akari[0][5]=2;
akari[5][6]=0;
break;
case 3 :
akari[0][0]=98;
akari[2][0]=98;
akari[3][0]=1;
akari[6][0]=98;
akari[6][2]=98;
akari[0][3]=1;
akari[3][3]=1;
akari[6][3]=98;
akari[0][4]=2;
akari[0][6]=98;
akari[3][6]=1;
akari[4][6]=0;
akari[6][6]=98;
break;
}
}




